home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Collection of Tools & Utilities
/
Collection of Tools and Utilities.iso
/
c
/
mnsbm.zip
/
DIR.CB
next >
Wrap
Text File
|
1989-10-31
|
25KB
|
909 lines
/* */
/* dir.m: */
/* A full-fledged, integrated environment for manipulating files */
/* and directories from within BRIEF. Uses the dialog manager. */
/* If sorting is desired, also pulls in sort macros. */
/* */
/* Written by Dan Teven. Preliminary version, August 6, 1987. */
/* Requires the dialog manager from BRIEF version 2.01. */
/* */
#include "dialog.h"
/* These definitions are the names of temporary files that the directory */
/* macro will use. You can change them to specify a full path name. */
/* For best performance, specify a temp file on a RAMdisk. */
#define TEMP_FILE "dir.tmp"
#define ERROR_FILE "dir.err"
#define TAG_CHAR "√"
/* These numbers define the column positions for the various fields */
/* of the directory listing (with the format defined here and no tabs). */
/* The full definition is a sort command for the buffer. */
#define NAME_SORT "_dir_sort"
#define EXT_SORT "_dir_sort 11"
#define SIZE_SORT "_dir_sort 15"
#define TIME_SORT "_dir_sort 25"
/* The tag column is with tabs 6. */
#define TAG_COL 47
/* */
/* _init: */
/* Initialize global variables. The only two global strings are */
/* used to hold the current directory path and file spec. */
/* */
_init ()
{
int _dir_buffer,
_dir_tags;
string _dir_wd,
_filespec;
global _dir_wd,
_filespec,
_dir_buffer,
_dir_tags;
}
/* */
/* dir: */
/* The directory macro. The syntax is dir [pathspec] where */
/* pathspec is an optional alternate path or file specifier to */
/* use. */
/* */
dir (...)
{
extern add_to_path;
int last_loc,
current_buffer;
string initial_wd;
/* Get the current directory so we can change back to it upon */
/* exiting. Make that the default. */
getwd ("", initial_wd);
_dir_wd = initial_wd;
/* If no directory spec passed on the command line, we use */
/* the default. We use the passed spec if possible. */
if (!get_parm (0, _filespec))
_filespec = _dir_wd;
/* Try to change immediately to the filespec entered as if */
/* it were a directory. If we can change to it, it becomes */
/* the default for future invocations. */
if (cd (_filespec))
{
getwd ("", _dir_wd);
_filespec = "*.*";
}
else
/* OK, so it wasn't just a directory. */
/* If we have a backslash in the path specifier, split it */
/* into a new directory specifier and file specifier. */
/* If not, it must be all file specifier, which implies */
/* use of the current directory. */
{
if (last_loc = search_string ("\\\\[~\\\\/]+", _filespec + "", TRUE))
{
_dir_wd = substr (_filespec, 1, last_loc - 1);
_filespec = substr (_filespec, last_loc + 1);
}
/* Try to change to the directory specified. If the cd */
/* fails, we know the directory is bad, and we reset it to */
/* the current directory and exit. */
if (cd (_dir_wd))
getwd ("", _dir_wd);
else
{
error ("Invalid path specified.");
beep ();
return FALSE;
}
}
message ("Creating directory list...");
/* Create a temporary file containing the directory listing */
/* for _filespec, and read it in. We are already in the */
/* directory referred to. */
current_buffer = inq_buffer ();
set_buffer (_dir_buffer = create_buffer ("Directory", NULL, TRUE));
dos ("dir " + (_filespec + (" >&" + TEMP_FILE)));
read_file (TEMP_FILE);
/* Determine if any files matched the file spec. If none did, */
/* we clean up and exit. */
if (search_back ("<File not found", TRUE, TRUE))
{
error ("No files found.");
beep ();
}
else
{
_reformat_directory ();
_process_menu (4, 20, 54, 2, "", "Press Alt-H for Help", NULL, _dir_buffer, "_dir_action", TRUE);
}
/* Delete the directory buffer, and the file(s) associated with */
/* it. Reset the packages for the current buffer. */
set_buffer (current_buffer);
call_registered_macro (1);
delete_buffer (_dir_buffer);
del (TEMP_FILE);
/* Change back to the original directory, and return. */
cd (initial_wd);
return TRUE;
}
/* */
/* _reformat_directory: */
/* Takes a buffer (named by the global variable _dir_buffer) and */
/* converts it to a more readable and useful menu-style format. */
/* */
_reformat_directory ()
{
string name;
/* Delete the first four lines of the file, which are two */
/* blank lines, the volume label, and the directory specifier. */
move_abs (4, 1);
drop_anchor (3);
top_of_buffer ();
delete_block ();
/* Insert a tab before every remaining line of the file. Then */
/* insert two delimiters into the file, and sort it so that */
/* all the directories are in one list and all the files are */
/* in the other. */
translate ("<{[~]*}>", "\\t\\0 ;_dir_tag_line", TRUE, TRUE);
insert ("Directories:\nFiles:\n");
while (search_fwd ("<DIR>", FALSE, TRUE))
{
drop_anchor (3);
cut ();
search_back ("<F", TRUE, TRUE);
paste ();
}
/* Remove the <DIR> marker for the directories, which we */
/* know are all before the current position at this point. */
/* Also, change the action to _dir_chdir. */
translate ("<DIR> ", " ", TRUE, FALSE, TRUE, FALSE, FALSE);
translate (";_dir_tag_line", ";_dir_chdir", TRUE, FALSE, TRUE, FALSE, FALSE);
/* Delete the last lines of the buffer, which are the */
/* number of files/free space and a blank line. */
end_of_buffer ();
up ();
delete_line ();
delete_line ();
tabs (6);
top_of_buffer ();
_dir_tags = 0;
}
/* */
/* _dir_action: */
/* Handles events passed to it by the dialog manager. */
/* */
_dir_action (...)
{
int event_type;
string button_text;
get_parm (0, event_type);
switch (event_type)
{
case DIALOG_INIT:
{
assign_to_key ("<Alt-A>", "_dir_tag_all");
assign_to_key ("<Alt-C>", "_dir_copy");
assign_to_key ("<Alt-D>", "_dir_delete");
assign_to_key ("<Alt-E>", "_for_tagged _dir_edit");
assign_to_key ("<Alt-H>", "_dir_help");
assign_to_key ("<Alt-N>", NAME_SORT);
assign_to_key ("<Alt-R>", "_for_tagged _dir_rename");
assign_to_key ("<Alt-S>", SIZE_SORT);
assign_to_key ("<Alt-T>", TIME_SORT);
assign_to_key ("<Alt-X>", EXT_SORT);
assign_to_key ("<Alt-Z>", "dos");
button_text = "Directory of " + _dir_wd;
/* If the directory name is the root, we don't */
/* add the \ before the file specifier. */
if (strlen (_dir_wd) > 3)
button_text += "\\";
message (button_text + (_filespec + "."));
}
case DIALOG_TERM:
{
assign_to_key ("<Alt-A>", "nothing");
assign_to_key ("<Alt-C>", "nothing");
assign_to_key ("<Alt-D>", "nothing");
assign_to_key ("<Alt-E>", "nothing");
assign_to_key ("<Alt-H>", "nothing");
assign_to_key ("<Alt-N>", "nothing");
assign_to_key ("<Alt-R>", "nothing");
assign_to_key ("<Alt-S>", "nothing");
assign_to_key ("<Alt-T>", "nothing");
assign_to_key ("<Alt-X>", "nothing");
assign_to_key ("<Alt-Z>", "nothing");
}
/* We check to make sure there's a semicolon on the line */
/* before we move to it. */
case DIALOG_ALTER_MENU:
{
get_parm (2, button_text);
if (!index (button_text, ";"))
return FALSE;
}
/* If a menu button is picked, we execute